$(function () {
    // IScroll
    // _loadingStep: 加载状态0默认，1显示加载状态，2执行加载数据，只有当为0时才能再次加载，这是防止过快拉动刷新
    var _myScroll, _pullUpEl, _pullUpL, _loadingStep = 0, isdisplayFilter = false;

    var discountPage = function () {
        this.init();
    };
    discountPage.prototype = {
        // 初始化页面
        init: function () {
            this.loadData();
            this.loadAdv();
            this.bindEvent();
            this.initIscroll();
        },

        // 绑定事件
        bindEvent: function () {
            // 点击后退
            $("#back").on("click", function () {
                back();
            });
        },

        // 加载数据
        loadData: function () {
            // 懒加载
            $('img.lazy').lazyload({
                placeholder: "../../images/white.gif",
                event: "scroll",
                threshold: 100,
                failure_limit: 10
            });
        },

        // 加载广告
        loadAdv: function () {
            // 滚动广告
            $(window).resize(function () {
                var width = $(window).width();
                $('div[name=touchslider-item] a').css('width', width);
                $('#touchslider-viewport').css('height', width / 3.85 - 15);
            }).resize();
            $("#touchslider").touchSlider({mouseTouch: true, autoplay: true});
        },


        // 初始化Iscroll
        initIscroll: function () {
            _pullUpEl = $('#pullUp');
            _pullUpL = _pullUpEl.find('._pullUpLabel');

            _myScroll = new IScroll('#wrapper', {
                probeType: 2,
                scrollbars: true,
                mouseWheel: false,
                fadeScrollbars: true,
                bounce: true,
                interactiveScrollbars: false,
                shrinkScrollbars: 'scale',
                click: true,
                keyBindings: true,
                momentum: true,
            });
            var model = this;
            //滚动时
            _myScroll.on('scroll', function () {
                if (_loadingStep == 0 && !_pullUpEl.attr('class').match('flip|loading')) {
                    if (this.y <= 5 && this.y < (this.maxScrollY - 5)) {
                        //上拉刷新效果
                        _pullUpEl.attr('class', _pullUpEl['class']);
                        _pullUpEl.show();
                        _myScroll.refresh();
                        _pullUpEl.addClass('flip');
                        _pullUpL.html('上拉显示更多');
                        _loadingStep = 1;
                    }
                }
            });

            //滚动完毕
            _myScroll.on('scrollEnd', function () {
                if (_loadingStep == 1) {
                    if (_pullUpEl.attr('class').match('flip|loading')) {
                        _pullUpEl.removeClass('flip').addClass('loading');
                        _pullUpL.html('努力的加载中...');
                        _loadingStep = 2;
                        pullUpAction();
                    }
                }

                // 懒加载
                $('img.lazy').lazyload({placeholder: "../../images/grey.gif", threshold: 100, failure_limit: 10});
            });

            // 下拉加载
            function pullUpAction() {
                setTimeout(function () {
                    // TODO

                    _pullUpEl.removeClass('loading');
                    _pullUpL.html('上拉显示更多...');
                    _myScroll.refresh();
                    _loadingStep = 0;
                }, 1000);
            }

            _myScroll.refresh();
            document.addEventListener('touchmove', function (e) {
                e.preventDefault();
            }, false);
        },
    };

    var discountPage = new discountPage();
});
